package com.ruoyi.manage.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.util.Calendar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.manage.mapper.CrInterviewAppointmentMapper;
import com.ruoyi.manage.mapper.CrInterviewSlotMapper;
import com.ruoyi.manage.mapper.CrApplicationMapper;
import com.ruoyi.manage.domain.CrInterviewAppointment;
import com.ruoyi.manage.domain.CrInterviewSlot;
import com.ruoyi.manage.domain.CrApplication;
import com.ruoyi.manage.service.ICrInterviewAppointmentService;

/**
 * 面试预约Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-24
 */
@Service
public class CrInterviewAppointmentServiceImpl implements ICrInterviewAppointmentService 
{
    @Autowired
    private CrInterviewAppointmentMapper crInterviewAppointmentMapper;

    @Autowired
    private CrInterviewSlotMapper crInterviewSlotMapper;
    
    @Autowired
    private CrApplicationMapper crApplicationMapper;

    /**
     * 查询面试预约
     * 
     * @param appointmentId 面试预约主键
     * @return 面试预约
     */
    @Override
    public CrInterviewAppointment selectCrInterviewAppointmentByAppointmentId(Long appointmentId)
    {
        return crInterviewAppointmentMapper.selectCrInterviewAppointmentByAppointmentId(appointmentId);
    }

    /**
     * 查询面试预约列表
     * 
     * @param crInterviewAppointment 面试预约
     * @return 面试预约
     */
    @Override
    public List<CrInterviewAppointment> selectCrInterviewAppointmentList(CrInterviewAppointment crInterviewAppointment)
    {
        return crInterviewAppointmentMapper.selectCrInterviewAppointmentList(crInterviewAppointment);
    }

    /**
     * 新增面试预约
     * 
     * @param crInterviewAppointment 面试预约
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCrInterviewAppointment(CrInterviewAppointment crInterviewAppointment)
    {
        crInterviewAppointment.setCreateTime(DateUtils.getNowDate());
        return crInterviewAppointmentMapper.insertCrInterviewAppointment(crInterviewAppointment);
    }

    /**
     * 修改面试预约
     * 
     * @param crInterviewAppointment 面试预约
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCrInterviewAppointment(CrInterviewAppointment crInterviewAppointment)
    {
        crInterviewAppointment.setUpdateTime(DateUtils.getNowDate());
        return crInterviewAppointmentMapper.updateCrInterviewAppointment(crInterviewAppointment);
    }

    /**
     * 批量删除面试预约
     * 
     * @param appointmentIds 需要删除的面试预约主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteCrInterviewAppointmentByAppointmentIds(Long[] appointmentIds)
    {
        return crInterviewAppointmentMapper.deleteCrInterviewAppointmentByAppointmentIds(appointmentIds);
    }

    /**
     * 删除面试预约信息
     * 
     * @param appointmentId 面试预约主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteCrInterviewAppointmentByAppointmentId(Long appointmentId)
    {
        return crInterviewAppointmentMapper.deleteCrInterviewAppointmentByAppointmentId(appointmentId);
    }

    /**
     * 根据学生ID查询预约列表
     * 
     * @param studentId 学生ID
     * @return 预约列表
     */
    @Override
    public List<CrInterviewAppointment> selectAppointmentsByStudentId(Long studentId)
    {
        return crInterviewAppointmentMapper.selectStudentInterviewAppointmentList(studentId);
    }

    /**
     * 根据社团ID查询预约列表
     * 
     * @param clubId 社团ID
     * @return 预约列表
     */
    @Override
    public List<CrInterviewAppointment> selectAppointmentsByClubId(Long clubId)
    {
        return crInterviewAppointmentMapper.selectClubInterviewAppointmentList(clubId, null);
    }

    /**
     * 根据面试时段ID查询预约列表
     * 
     * @param slotId 面试时段ID
     * @return 预约列表
     */
    @Override
    public List<CrInterviewAppointment> selectAppointmentsBySlotId(Long slotId)
    {
        return crInterviewAppointmentMapper.selectAppointmentsBySlotId(slotId);
    }

    /**
     * 学生预约面试
     * 
     * @param studentId 学生ID
     * @param slotId 面试时段ID
     * @param applicationId 申请ID
     * @param remarks 备注
     * @return 结果
     */
    @Override
    @Transactional
    public int bookInterview(Long studentId, Long slotId, Long applicationId, String remarks)
    {
        // 验证时段
        CrInterviewSlot slot = crInterviewSlotMapper.selectCrInterviewSlotBySlotId(slotId);
        if (slot == null) {
            throw new RuntimeException("面试时段不存在");
        }
        if (!"0".equals(slot.getStatus())) {
            throw new RuntimeException("该面试时段不可预约");
        }

        // 检查时段容量
        int bookedCount = slot.getBookedCount() != null ? slot.getBookedCount() : 0;
        int maxCapacity = slot.getMaxApplicants() != null ? slot.getMaxApplicants() : 0;
        if (bookedCount >= maxCapacity) {
            throw new RuntimeException("该面试时段已满，请选择其他时段");
        }

        // 检查重复预约
        CrInterviewAppointment existingAppointment = 
            crInterviewAppointmentMapper.selectAppointmentByUserAndSlot(studentId, slotId);
        if (existingAppointment != null) {
            throw new RuntimeException("您已预约该面试时段");
        }

        // 检查时间冲突
        if (checkStudentTimeConflict(studentId, slotId)) {
            throw new RuntimeException("该时间段与您的其他面试预约冲突");
        }

        // 处理application_id：如果为空，创建一个基础申请记录
        if (applicationId == null) {
            // 通过slot获取recruitment信息
            if (slot.getRecruitmentId() == null) {
                throw new RuntimeException("该面试时段未关联招聘信息，无法预约");
            }
            
            // 创建基础申请记录用于面试预约
            CrApplication application = new CrApplication();
            application.setRecruitmentId(slot.getRecruitmentId());
            application.setUserId(studentId);
            application.setClubId(slot.getClubId());
            application.setApplicationContent("通过面试时段直接预约");
            application.setStatus("1"); // 面试中状态
            application.setCreateTime(DateUtils.getNowDate());
            
            int result = crApplicationMapper.insertCrApplication(application);
            if (result <= 0) {
                throw new RuntimeException("创建申请记录失败");
            }
            applicationId = application.getApplicationId();
        } else {
            // 验证现有申请ID
            CrApplication application = crApplicationMapper.selectCrApplicationByApplicationId(applicationId);
            if (application == null || !application.getUserId().equals(studentId)) {
                throw new RuntimeException("申请信息不存在或不属于当前用户");
            }
            // 修改状态判断，允许面试中(1)和已通过(2)两种状态预约面试，与前端保持一致
            if (!"1".equals(application.getStatus()) && !"2".equals(application.getStatus())) {
                throw new RuntimeException("只有审核通过的申请才能预约面试");
            }
        }

        // 创建预约
        CrInterviewAppointment appointment = new CrInterviewAppointment();
        appointment.setUserId(studentId);
        appointment.setSlotId(slotId);
        appointment.setApplicationId(applicationId);
        appointment.setClubId(slot.getClubId());
        appointment.setStatus("0"); // 待确认
        appointment.setCreateTime(DateUtils.getNowDate());

        int result = crInterviewAppointmentMapper.insertCrInterviewAppointment(appointment);

        // 更新时段预约数量
        if (result > 0) {
            crInterviewSlotMapper.updateBookedCount(slotId, 1);
        }

        return result;
    }

    /**
     * 取消面试预约
     * 
     * @param appointmentId 预约ID
     * @param userId 用户ID
     * @param reason 取消原因
     * @return 结果
     */
    @Override
    @Transactional
    public int cancelAppointment(Long appointmentId, Long userId, String reason)
    {
        CrInterviewAppointment appointment = 
            crInterviewAppointmentMapper.selectCrInterviewAppointmentByAppointmentId(appointmentId);
        
        if (appointment == null) {
            throw new RuntimeException("预约信息不存在");
        }

        if (!appointment.getUserId().equals(userId)) {
            throw new RuntimeException("只能取消自己的预约");
        }

        if ("3".equals(appointment.getStatus()) || "4".equals(appointment.getStatus())) {
            throw new RuntimeException("该预约已取消或已完成，无法再次取消");
        }

        // 检查取消时间限制（面试前2小时不允许取消）
        CrInterviewSlot slot = crInterviewSlotMapper.selectCrInterviewSlotBySlotId(appointment.getSlotId());
        if (slot != null && slot.getInterviewDate() != null) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(slot.getInterviewDate());
            cal.add(Calendar.HOUR_OF_DAY, -2);
            
            if (new Date().after(cal.getTime())) {
                throw new RuntimeException("面试前2小时不允许取消预约");
            }
        }

        appointment.setStatus("3"); // 已取消
        appointment.setUpdateTime(DateUtils.getNowDate());

        int result = crInterviewAppointmentMapper.updateCrInterviewAppointment(appointment);

        // 减少时段预约数量
        if (result > 0) {
            crInterviewSlotMapper.updateBookedCount(appointment.getSlotId(), -1);
        }

        return result;
    }

    /**
     * 确认面试预约
     * 
     * @param appointmentId 预约ID
     * @param clubId 社团ID
     * @return 结果
     */
    @Override
    @Transactional
    public int confirmAppointment(Long appointmentId, Long clubId)
    {
        CrInterviewAppointment appointment = 
            crInterviewAppointmentMapper.selectCrInterviewAppointmentByAppointmentId(appointmentId);
        
        if (appointment == null) {
            throw new RuntimeException("预约信息不存在");
        }

        if (!appointment.getClubId().equals(clubId)) {
            throw new RuntimeException("只能确认本社团的预约");
        }

        if (!"0".equals(appointment.getStatus())) {
            throw new RuntimeException("只能确认待确认状态的预约");
        }

        appointment.setStatus("1"); // 已确认
        appointment.setUpdateTime(DateUtils.getNowDate());

        return crInterviewAppointmentMapper.updateCrInterviewAppointment(appointment);
    }

    /**
     * 完成面试并评价
     * 
     * @param appointmentId 预约ID
     * @param clubId 社团ID
     * @param interviewResult 面试结果
     * @param interviewScore 面试得分
     * @param interviewFeedback 面试反馈
     * @return 结果
     */
    @Override
    @Transactional
    public int completeInterview(Long appointmentId, Long clubId, String interviewResult, 
                                Integer interviewScore, String interviewFeedback)
    {
        CrInterviewAppointment appointment = 
            crInterviewAppointmentMapper.selectCrInterviewAppointmentByAppointmentId(appointmentId);
        
        if (appointment == null) {
            throw new RuntimeException("预约信息不存在");
        }

        if (!appointment.getClubId().equals(clubId)) {
            throw new RuntimeException("只能完成本社团的面试");
        }

        if (!"1".equals(appointment.getStatus()) && !"2".equals(appointment.getStatus())) {
            throw new RuntimeException("只能完成已确认或进行中的面试");
        }

        // 验证评分
        if (interviewScore != null && (interviewScore < 0 || interviewScore > 100)) {
            throw new RuntimeException("面试评分必须在0-100之间");
        }

        appointment.setStatus("4"); // 已完成
        appointment.setScore(interviewScore != null ? new BigDecimal(interviewScore) : null);
        appointment.setEvaluation(interviewFeedback);
        appointment.setActualEndTime(DateUtils.getNowDate());
        appointment.setUpdateTime(DateUtils.getNowDate());

        return crInterviewAppointmentMapper.updateCrInterviewAppointment(appointment);
    }

    /**
     * 学生对面试进行反馈
     * 
     * @param appointmentId 预约ID
     * @param studentId 学生ID
     * @param studentFeedback 学生反馈
     * @param satisfactionRating 满意度评分
     * @return 结果
     */
    @Override
    @Transactional
    public int submitStudentFeedback(Long appointmentId, Long studentId, String studentFeedback, 
                                    Integer satisfactionRating)
    {
        CrInterviewAppointment appointment = 
            crInterviewAppointmentMapper.selectCrInterviewAppointmentByAppointmentId(appointmentId);
        
        if (appointment == null) {
            throw new RuntimeException("预约信息不存在");
        }

        if (!appointment.getUserId().equals(studentId)) {
            throw new RuntimeException("只能对自己的面试进行反馈");
        }

        if (!"4".equals(appointment.getStatus())) {
            throw new RuntimeException("只能对已完成的面试进行反馈");
        }

        // 验证满意度评分
        if (satisfactionRating != null && (satisfactionRating < 1 || satisfactionRating > 5)) {
            throw new RuntimeException("满意度评分必须在1-5之间");
        }

        appointment.setStudentFeedback(studentFeedback);
        appointment.setUpdateTime(DateUtils.getNowDate());

        return crInterviewAppointmentMapper.updateCrInterviewAppointment(appointment);
    }

    /**
     * 重新安排面试时间
     * 
     * @param appointmentId 预约ID
     * @param newSlotId 新时段ID
     * @param rescheduleReason 重新安排原因
     * @return 结果
     */
    @Override
    @Transactional
    public int rescheduleAppointment(Long appointmentId, Long newSlotId, String rescheduleReason)
    {
        CrInterviewAppointment appointment = 
            crInterviewAppointmentMapper.selectCrInterviewAppointmentByAppointmentId(appointmentId);
        
        if (appointment == null) {
            throw new RuntimeException("预约信息不存在");
        }

        if ("3".equals(appointment.getStatus()) || "4".equals(appointment.getStatus())) {
            throw new RuntimeException("已取消或已完成的预约无法重新安排");
        }

        // 验证新时段
        CrInterviewSlot newSlot = crInterviewSlotMapper.selectCrInterviewSlotBySlotId(newSlotId);
        if (newSlot == null || !"0".equals(newSlot.getStatus())) {
            throw new RuntimeException("新面试时段不可用");
        }

        // 检查新时段容量
        int bookedCount = newSlot.getBookedCount() != null ? newSlot.getBookedCount() : 0;
        int maxCapacity = newSlot.getMaxApplicants() != null ? newSlot.getMaxApplicants() : 0;
        if (bookedCount >= maxCapacity) {
            throw new RuntimeException("新面试时段已满");
        }

        // 释放原时段容量
        crInterviewSlotMapper.updateBookedCount(appointment.getSlotId(), -1);

        // 更新预约信息
        appointment.setSlotId(newSlotId);
        appointment.setStatus("0"); // 重置为待确认
        appointment.setUpdateTime(DateUtils.getNowDate());

        int result = crInterviewAppointmentMapper.updateCrInterviewAppointment(appointment);

        // 占用新时段容量
        if (result > 0) {
            crInterviewSlotMapper.updateBookedCount(newSlotId, 1);
        }

        return result;
    }

    /**
     * 批量确认预约
     * 
     * @param appointmentIds 预约ID数组
     * @param clubId 社团ID
     * @return 结果
     */
    @Override
    @Transactional
    public int batchConfirmAppointments(Long[] appointmentIds, Long clubId)
    {
        if (appointmentIds == null || appointmentIds.length == 0) {
            throw new RuntimeException("请选择要确认的预约");
        }

        int successCount = 0;
        for (Long appointmentId : appointmentIds) {
            try {
                confirmAppointment(appointmentId, clubId);
                successCount++;
            } catch (Exception e) {
                // 继续处理其他预约
                continue;
            }
        }

        return successCount;
    }

    /**
     * 检查学生是否有时间冲突
     * 
     * @param studentId 学生ID
     * @param slotId 面试时段ID
     * @return 是否冲突
     */
    @Override
    public boolean checkStudentTimeConflict(Long studentId, Long slotId)
    {
        // 获取该时段的时间信息
        CrInterviewSlot slot = crInterviewSlotMapper.selectCrInterviewSlotBySlotId(slotId);
        if (slot == null) {
            return false;
        }

        // 查询学生的所有预约
        List<CrInterviewAppointment> appointments = 
            crInterviewAppointmentMapper.selectStudentInterviewAppointmentList(studentId);

        // 检查是否有在同一天的其他预约
        for (CrInterviewAppointment appointment : appointments) {
            if ("3".equals(appointment.getStatus()) || "4".equals(appointment.getStatus())) {
                continue; // 跳过已取消或已完成的预约
            }

            CrInterviewSlot existingSlot = crInterviewSlotMapper.selectCrInterviewSlotBySlotId(appointment.getSlotId());
            if (existingSlot != null && existingSlot.getInterviewDate() != null && slot.getInterviewDate() != null) {
                String existingDate = DateUtils.parseDateToStr("yyyy-MM-dd", existingSlot.getInterviewDate());
                String newDate = DateUtils.parseDateToStr("yyyy-MM-dd", slot.getInterviewDate());
                
                if (existingDate.equals(newDate)) {
                    // 同一天，检查时间是否重叠
                    if (isTimeOverlap(existingSlot.getStartTime(), existingSlot.getEndTime(),
                                    slot.getStartTime(), slot.getEndTime())) {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    /**
     * 检查面试官是否有时间冲突
     * 
     * @param interviewerId 面试官ID
     * @param slotId 面试时段ID
     * @return 是否冲突
     */
    @Override
    public boolean checkInterviewerTimeConflict(Long interviewerId, Long slotId)
    {
        // 此方法的具体实现需要根据面试官信息的存储方式来确定
        // 这里提供一个基础框架
        return false;
    }

    /**
     * 获取学生的预约统计
     * 
     * @param studentId 学生ID
     * @return 统计信息
     */
    @Override
    public Map<String, Object> getStudentAppointmentStatistics(Long studentId)
    {
        Map<String, Object> statistics = new HashMap<>();
        
        List<CrInterviewAppointment> appointments = 
            crInterviewAppointmentMapper.selectStudentInterviewAppointmentList(studentId);
        statistics.put("totalAppointments", appointments.size());

        // 按状态统计
        long pendingCount = appointments.stream().filter(app -> "0".equals(app.getStatus())).count();
        long confirmedCount = appointments.stream().filter(app -> "1".equals(app.getStatus())).count();
        long completedCount = appointments.stream().filter(app -> "4".equals(app.getStatus())).count();
        long cancelledCount = appointments.stream().filter(app -> "3".equals(app.getStatus())).count();

        statistics.put("pendingCount", pendingCount);
        statistics.put("confirmedCount", confirmedCount);
        statistics.put("completedCount", completedCount);
        statistics.put("cancelledCount", cancelledCount);

        return statistics;
    }

    /**
     * 获取社团的预约统计
     * 
     * @param clubId 社团ID
     * @return 统计信息
     */
    @Override
    public Map<String, Object> getClubAppointmentStatistics(Long clubId)
    {
        Map<String, Object> statistics = new HashMap<>();
        
        List<CrInterviewAppointment> appointments = 
            crInterviewAppointmentMapper.selectClubInterviewAppointmentList(clubId, null);
        statistics.put("totalAppointments", appointments.size());

        // 按状态统计
        long pendingCount = appointments.stream().filter(app -> "0".equals(app.getStatus())).count();
        long confirmedCount = appointments.stream().filter(app -> "1".equals(app.getStatus())).count();
        long completedCount = appointments.stream().filter(app -> "4".equals(app.getStatus())).count();
        long cancelledCount = appointments.stream().filter(app -> "3".equals(app.getStatus())).count();

        statistics.put("pendingCount", pendingCount);
        statistics.put("confirmedCount", confirmedCount);
        statistics.put("completedCount", completedCount);
        statistics.put("cancelledCount", cancelledCount);

        // 今日面试统计
        List<CrInterviewAppointment> todayAppointments = 
            crInterviewAppointmentMapper.selectTodayInterviewAppointments(clubId);
        statistics.put("todayAppointments", todayAppointments.size());

        return statistics;
    }

    /**
     * 获取面试时段的预约统计
     * 
     * @param slotId 面试时段ID
     * @return 统计信息
     */
    @Override
    public Map<String, Object> getSlotAppointmentStatistics(Long slotId)
    {
        Map<String, Object> statistics = new HashMap<>();
        
        List<CrInterviewAppointment> appointments = 
            crInterviewAppointmentMapper.selectAppointmentsBySlotId(slotId);
        statistics.put("totalAppointments", appointments.size());

        // 按状态统计
        long pendingCount = appointments.stream().filter(app -> "0".equals(app.getStatus())).count();
        long confirmedCount = appointments.stream().filter(app -> "1".equals(app.getStatus())).count();
        long completedCount = appointments.stream().filter(app -> "4".equals(app.getStatus())).count();

        statistics.put("pendingCount", pendingCount);
        statistics.put("confirmedCount", confirmedCount);
        statistics.put("completedCount", completedCount);

        return statistics;
    }

    /**
     * 查询需要提醒的预约
     * 
     * @param hours 提前小时数
     * @return 预约列表
     */
    @Override
    public List<CrInterviewAppointment> getAppointmentsForReminder(int hours)
    {
        return crInterviewAppointmentMapper.selectUpcomingInterviews(hours);
    }

    /**
     * 发送预约提醒
     * 
     * @param appointmentId 预约ID
     * @return 结果
     */
    @Override
    public int sendAppointmentReminder(Long appointmentId)
    {
        // 这里可以集成短信或邮件发送功能
        // 现在只是标记为已提醒
        CrInterviewAppointment appointment = 
            crInterviewAppointmentMapper.selectCrInterviewAppointmentByAppointmentId(appointmentId);
        
        if (appointment != null) {
            appointment.setUpdateTime(DateUtils.getNowDate());
            return crInterviewAppointmentMapper.updateCrInterviewAppointment(appointment);
        }
        
        return 0;
    }

    /**
     * 批量发送预约提醒
     * 
     * @param appointmentIds 预约ID数组
     * @return 结果
     */
    @Override
    public int batchSendReminders(Long[] appointmentIds)
    {
        if (appointmentIds == null || appointmentIds.length == 0) {
            return 0;
        }

        int successCount = 0;
        for (Long appointmentId : appointmentIds) {
            if (sendAppointmentReminder(appointmentId) > 0) {
                successCount++;
            }
        }

        return successCount;
    }

    /**
     * 查询过期未处理的预约
     * 
     * @return 预约列表
     */
    @Override
    public List<CrInterviewAppointment> getExpiredAppointments()
    {
        CrInterviewAppointment queryParam = new CrInterviewAppointment();
        queryParam.setStatus("0"); // 待确认状态
        
        List<CrInterviewAppointment> appointments = 
            crInterviewAppointmentMapper.selectCrInterviewAppointmentList(queryParam);

        // 过滤过期的预约（这里简化处理，实际应该在SQL中处理）
        return appointments.stream()
                .filter(appointment -> {
                    // 检查创建时间是否超过24小时未确认
                    long diffHours = (new Date().getTime() - appointment.getCreateTime().getTime()) / (1000 * 60 * 60);
                    return diffHours > 24;
                })
                .collect(java.util.stream.Collectors.toList());
    }

    /**
     * 自动处理过期预约
     * 
     * @return 处理数量
     */
    @Override
    @Transactional
    public int processExpiredAppointments()
    {
        List<CrInterviewAppointment> expiredAppointments = getExpiredAppointments();
        
        int processedCount = 0;
        for (CrInterviewAppointment appointment : expiredAppointments) {
            try {
                appointment.setStatus("3"); // 设置为已取消
                appointment.setUpdateTime(DateUtils.getNowDate());
                
                if (crInterviewAppointmentMapper.updateCrInterviewAppointment(appointment) > 0) {
                    // 释放时段容量
                    crInterviewSlotMapper.updateBookedCount(appointment.getSlotId(), -1);
                    processedCount++;
                }
            } catch (Exception e) {
                continue;
            }
        }

        return processedCount;
    }

    /**
     * 导出预约数据
     * 
     * @param crInterviewAppointment 查询条件
     * @return 预约列表
     */
    @Override
    public List<CrInterviewAppointment> exportAppointments(CrInterviewAppointment crInterviewAppointment)
    {
        return crInterviewAppointmentMapper.selectCrInterviewAppointmentList(crInterviewAppointment);
    }

    /**
     * 辅助方法：检查时间是否重叠
     */
    private boolean isTimeOverlap(String start1, String end1, String start2, String end2) {
        try {
            int start1Int = Integer.parseInt(start1.replace(":", ""));
            int end1Int = Integer.parseInt(end1.replace(":", ""));
            int start2Int = Integer.parseInt(start2.replace(":", ""));
            int end2Int = Integer.parseInt(end2.replace(":", ""));

            return !(end1Int <= start2Int || end2Int <= start1Int);
        } catch (Exception e) {
            return false;
        }
    }
}